package com.swiftops.basic.sys.service.impl;

import com.swiftops.common.service.AbstractService;
import com.swiftops.common.util.ListUtil;
import com.swiftops.basic.sys.dao.SysRoleMenuMapper;
import com.swiftops.basic.sys.model.SysRoleMenu;
import com.swiftops.basic.sys.service.SysRoleMenuService;
import com.swiftops.basic.sys.vo.entity.SysRoleMenuEntityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 角色菜单关系
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysRoleMenuServiceImpl extends AbstractService<SysRoleMenu> implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    /**
     * 获取角色菜单关系详细信息
     *
     * @param roleId 角色菜单关系ROLE_ID
     * @return 角色菜单关系
     */
    @Override
    public List<SysRoleMenuEntityVO> getSysRoleMenuListByRoleId(String roleId) {
        Condition condition = new Condition(SysRoleMenu.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("roleId", roleId);
        List<SysRoleMenu> list = this.selectByCondition(condition);
        List<SysRoleMenuEntityVO> voList = ListUtil.convertList(list, SysRoleMenuEntityVO.class);
        return voList;
    }

    /**
     * 替换角色菜单关系
     *
     * @param list 角色菜单关系实体列表
     * @param roleId 角色菜单关系ROLE_ID
     */
    @Override
    public void replaceSysRoleMenu(String roleId, List<SysRoleMenuEntityVO> list) {
        Condition condition = new Condition(SysRoleMenu.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("roleId", roleId);
        this.deleteByCondition(condition);
        //
        if (ListUtil.isNotEmpty(list)) {
            List<SysRoleMenu> convertList = ListUtil.convertList(list, SysRoleMenu.class);
            this.sysRoleMenuMapper.batchInsertSysRoleMenu(convertList);
        }
    }

}
